package com.example.asus.homework11.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.asus.homework11.BaseListner;
import com.example.asus.homework11.BaseModel;
import com.example.asus.homework11.R;
import com.example.asus.homework11.Url;
import com.example.asus.homework11.adapter.FoodCommentAdapter;
import com.example.asus.homework11.bean.CommentBean;
import com.example.asus.homework11.bean.FoodListBean;
import com.example.asus.homework11.bean.ResultBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;

public class FoodContentActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_foodcontent_name,tv_foodcontent_price,tv_foodcontent_intro;
    private ImageView iv_foodcontent,iv_foodcontent_collect;
    private Button bt_foodcontent_gouwuche,bt_foodcontent_buy;
    private String foodname,foodpic,foodIntro;
    private int foodprice,foodid,userid;
    private SharedPreferences sharedPreferences;
    private boolean iscollect;
    private RecyclerView recyclerView;
    private List<FoodListBean> beans;
    private FoodCommentAdapter adapter;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_content);
        initView();
        initEvent();
        initData();
    }

    private void initView(){
        back = findViewById(R.id.back);
        tv_foodcontent_name = findViewById(R.id.tv_foodcontent_name);
        tv_foodcontent_price = findViewById(R.id.tv_foodcontent_price);
        iv_foodcontent = findViewById(R.id.iv_foodcontent);
        iv_foodcontent_collect = findViewById(R.id.iv_foodcontent_collect);
        bt_foodcontent_gouwuche = findViewById(R.id.bt_foodcontent_gouwuche);
        bt_foodcontent_buy = findViewById(R.id.bt_foodcontent_buy);
        recyclerView=findViewById(R.id.recyclerView2);
        tv_foodcontent_intro= findViewById(R.id.tv_foodcontent_intro);
        adapter=new FoodCommentAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    private void initEvent(){
        bt_foodcontent_gouwuche.setOnClickListener(this);
        bt_foodcontent_buy.setOnClickListener(this);
        iv_foodcontent_collect.setOnClickListener(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(FoodContentActivity.this,HomeActivity.class);
                startActivity(intent1);
            }
        });
    }

    private void initData(){
        sharedPreferences = getSharedPreferences("User",MODE_PRIVATE);
        userid = Integer.parseInt(sharedPreferences.getString("userid","获取id失败"));
        Intent intent = getIntent();
        foodid = intent.getIntExtra("foodid",0);
        foodprice = intent.getIntExtra("foodprice",0);
        foodpic = intent.getStringExtra("foodpic");
        foodname = intent.getStringExtra("foodname");
        foodIntro = intent.getStringExtra("foodintro");
        tv_foodcontent_name.setText(foodname);
        tv_foodcontent_price.setText(foodprice+"");
        tv_foodcontent_intro.setText(foodIntro);
        Picasso.get().load(Url.BASEUSRL+foodpic).into(iv_foodcontent);

        isCollect();
        getComments();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_foodcontent_gouwuche:

                break;
            case R.id.bt_foodcontent_buy:
                Intent intent = new Intent(FoodContentActivity.this,ConfirmOrderActivity.class);
                intent.putExtra("foodname",foodname);
                intent.putExtra("foodprice",foodprice);
                intent.putExtra("foodid",foodid);
                intent.putExtra("userid",userid);
                startActivity(intent);
                break;
            case R.id.iv_foodcontent_collect:
                doCollect();
                break;

        }
    }

    //判断是否收藏
    private void isCollect(){
        BaseModel<ResultBean> model = new BaseModel<>();
        Call<ResultBean> call = model.service.isCollected(userid,foodid,1);
        model.callEnqueue(call, new BaseListner<ResultBean>() {
            @Override
            public void onResponse(ResultBean resultBean) {
                if (resultBean.getCollected().equals("0")) {
                    iscollect = false;
                    iv_foodcontent_collect.setImageResource(R.drawable.ic_star_border);
                }
                else {
                    iscollect = true;
                    iv_foodcontent_collect.setImageResource(R.drawable.ic_star_yellow_24dp);
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(FoodContentActivity.this,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    //收藏/取消收藏
    private void doCollect(){
        BaseModel<ResultBean> model = new BaseModel<>();
        model.callEnqueue(model.service.doCollectFood(userid, foodid), new BaseListner<ResultBean>() {
            @Override
            public void onResponse(ResultBean resultBean) {
                if(resultBean.getSuccess().equals("0"))
                    Toast.makeText(FoodContentActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                else {
                    if(iscollect) {
                        iscollect = false;
                        iv_foodcontent_collect.setImageResource(R.drawable.ic_star_border);
                        Toast.makeText(FoodContentActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        iscollect = true;
                        iv_foodcontent_collect.setImageResource(R.drawable.ic_star_yellow_24dp);
                        Toast.makeText(FoodContentActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    }
                }
            }
            @Override
            public void onFail(String msg) {
                Toast.makeText(FoodContentActivity.this,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    //获取评论
    private void getComments(){
        BaseModel<List<CommentBean>> model = new BaseModel<>();
        model.callEnqueue(model.service.getComments(foodid), new BaseListner<List<CommentBean>>() {
            @Override
            public void onResponse(List<CommentBean> resultBean) {
                if(resultBean!=null){
                    adapter.setData(resultBean);
                }

                else {
                }
            }
            @Override
            public void onFail(String msg) {
            }
        });
    }
}
